package com.jie.deletefile.DongtaiVp;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jie.deletefile.R;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class DVpActivity extends AppCompatActivity {

    private ViewPager vp1,vp2;
    private List<String> data;
    private List<View> views;
    private ScheduledExecutorService service;
    private int currentItem = 0;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            vp1.setCurrentItem(currentItem);

        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dvp);
        initView();
        initData();
        initViewData();
        vp1.setAdapter(new MyVpAdapter());
        startAd();
        vp2 = (ViewPager) findViewById(R.id.vp2);
        new DVpUtil(this,null, vp2,  data).show();

    }

    private void startAd() {
        service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(new ScrollTask(), 3, 5, TimeUnit.SECONDS);
    }
    private class ScrollTask implements Runnable {

        @Override
        public void run() {
            try {
                synchronized (vp1) {
                    currentItem = (currentItem + 1) % views.size();
                    // currentItem = (currentItem + 1) % imageViews.size();
                    handler.obtainMessage().sendToTarget();
                }
            } catch (Throwable e) {
                // TODO: handle exception
            }

        }
    }

    private void initViewData() {
        views = new ArrayList<>();
        for (int i = 0;i<data.size();i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.dvp_item, null);
            ImageView iv = (ImageView) view.findViewById(R.id.dvp_i);

            x.image().bind(iv, data.get(i));
            views.add(view);
        }

    }

    private void initData() {
        data = new ArrayList<>();
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/44621585.jpg");
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/41331577.jpg");
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/98887668.jpg");
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/14976634.jpg");
        data.add("http://7xsct4.com1.z0.glb.clouddn.com/16-9-21/65897578.jpg");


    }

    private void initView() {
        vp1 = (ViewPager) findViewById(R.id.vp11);

    }

    class MyVpAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));

            return views.get(position);
        }
    }

}
